package com.imooc.article.html;

import com.imooc.api.config.RabbitMQConfig;
import com.imooc.article.html.controller.ArticleHTMLComponent;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.FileNotFoundException;

/**
 * @Description :
 * @Author : YTY
 * @Date : 2020-12-08 17:45
 */

@Component
public class RabbitMQConsumer {

    @Autowired
    private ArticleHTMLComponent articleHTMLComponent;

    @RabbitListener(queues = {RabbitMQConfig.QUEUE_DOWNLOAD_HTML})
    public void watchQueue(String payload, Message message) {

        System.out.println(payload);

        String routingKey =
                message.getMessageProperties().getReceivedRoutingKey();
        if (("article.publish.download.do").equalsIgnoreCase(routingKey)) {
            System.out.println("article.publish.download.do");
        } else if (("article.success.do").equalsIgnoreCase(routingKey)) {
            System.out.println("article.success.do");
        } else if (("article.download.do").equalsIgnoreCase(routingKey)) {
//            System.out.println("article.success.do");

            String articleId = payload.split(",")[0];
            String articleMongoId = payload.split(",")[1];

            try {
                articleHTMLComponent.download(articleId, articleMongoId);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }

        } else if (("article.delete.do").equalsIgnoreCase(routingKey)) {

            String articleId = payload;
            try {
                articleHTMLComponent.delete(articleId);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        } else {
            System.out.println("不符合规则的: " + routingKey);
        }
    }

}
